package hn.sigit.dao.hnd.ladmshadow;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import hn.sigit.model.hnd.ladmshadow.BAUnit;
import hn.sigit.model.hnd.ladmshadow.RRR;
import hn.sigit.dao.SigitDAO;

public class RRRDAO extends SigitDAO<RRR> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static final String deleteHQL = "delete RRR r where r.baunit = :baUnit and r.presentationId = :pID";
	private static String deleteRRRsHQL = "DELETE FROM RRR WHERE presentationId = :presentationId";
	private static String deleteRRRsHQL2 = "DELETE FROM RRR WHERE presentationId = :presentationId AND snapshot = :snapshot";
	
	protected RRRDAO() {
	}
	
	public static List<RRR> loadRRRsByPresentationId(long presentationId) {
		return loadRRRsByPresentationId(getSession(), presentationId);
	}
	public static List<RRR> loadRRRsByPresentationId(long presentationId, boolean onlySnapshots) {
		return loadRRRsByPresentationId(getSession(), presentationId, onlySnapshots);
	}
	public static int clearRRRsAssociationsByPresentationId(long presentationId, boolean includeSnapshot) {
		return clearRRRsAssociationsByPresentationId(getSession(), presentationId, includeSnapshot);
	}
	public static int deleteRRRsByPresentationId(long presentationId, boolean includeSnapshot) {
		return deleteRRRsByPresentationId(getSession(), presentationId, includeSnapshot);
	}
	public static void deleteByBaUnitIdAndPresentationId(long baUnitId, long presentationId) {
		deleteByBaUnitIdAndPresentationId(getSession(), baUnitId, presentationId);
	}

	
	@SuppressWarnings("unchecked")
	public static List<RRR> loadRRRsByPresentationId(Session session, long presentationId) {
		Criteria criteria = session.createCriteria(RRR.class);
		criteria.add(Restrictions.eq("presentationId", presentationId));
		
		return (List<RRR>)criteria.list();
	}
	@SuppressWarnings("unchecked")
	public static List<RRR> loadRRRsByPresentationId(Session session, long presentationId, boolean onlySnapshots) {
		Criteria criteria = session.createCriteria(RRR.class);
		criteria.add(Restrictions.eq("presentationId", presentationId));
		if (onlySnapshots)
			criteria.add( Restrictions.eq("snapshot", onlySnapshots) );
		
		return (List<RRR>)criteria.list();
	}
	public static int clearRRRsAssociationsByPresentationId(Session session, long presentationId, boolean includeSnapshot) {
		Criteria criteria = session.createCriteria(RRR.class);
		criteria.add(Restrictions.eq("presentationId", presentationId));
		if (!includeSnapshot)
			criteria.add(Restrictions.eq("snapshot", false));
		
		ScrollableResults rrrCursor = criteria.scroll();
		
		RRR rrr;
		int count = 0;
		while (rrrCursor.next()) {
			rrr = (RRR) rrrCursor.get(0);
			
			rrr.setParty(null);
			rrr.setBaunit(null);
			rrr.setAdminSources(null);
			
			if (++count % 100 == 0) {
				session.flush();
				session.clear();
			}
		}
		
		rrrCursor.close();

		return count;
	}
	public static int deleteRRRsByPresentationId(Session session, long presentationId, boolean includeSnapshot) {
		if (includeSnapshot) {
			return session.createQuery(deleteRRRsHQL)
				.setLong("presentationId", presentationId)
				.executeUpdate();
			
		}
		return session.createQuery(deleteRRRsHQL2)
			.setLong("presentationId", presentationId)
			.setBoolean("snapshot", false)
			.executeUpdate();
	}
	public static void deleteByBaUnitIdAndPresentationId(Session session, long baUnitId, long presentationId) {
		BAUnit baUnit = BAUnitDAO.loadBAUnitByLadmIdAndPresentationId(session, baUnitId, presentationId);
		
		Query q = session.createQuery(deleteHQL)
			.setParameter("baUnit", baUnit)
			.setParameter("pID", presentationId);
		
		q.executeUpdate();
	}
}
